package com.wwwarehouse.resource_center.adapter.createobject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.CategoryBean;
import com.wwwarehouse.resource_center.eventbus_event.CategoryEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/14.
 *         <p/>
 */

public class ChooseCategoryAdapter extends RecyclerView.Adapter<ChooseCategoryAdapter.ViewHolder> {

    private Context mContext = null;
    private List<CategoryBean.EmptyIdentifierBean> tagList = new ArrayList<>();
    private List<CategoryBean.EmptyIdentifierBean> selectList;
    private OnItemLongclickListener onItemLongclickListener;

    public ChooseCategoryAdapter(Context context, List<CategoryBean.EmptyIdentifierBean> list) {
        this.mContext = context;
        this.tagList = list;
        this.selectList = new ArrayList<>();

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_goods_category, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        try {

            holder.mTextView.setText(tagList.get(position).getName());
//            树包含的类目的来源：0,网仓;1,平台;2用户  1是叶子节点 0不是
            if ("0".equals(tagList.get(position).getSource()) && "0".equals(tagList.get(position).getLeafNode())) {
                holder.mIcon.setImageResource(R.drawable.wc_src);
                holder.mArrow.setVisibility(View.VISIBLE);
            } else if ("0".equals(tagList.get(position).getSource()) && ("1".equals(tagList.get(position).getLeafNode()))) {
                holder.mIcon.setImageResource(R.drawable.wc_leaf);
                holder.mArrow.setVisibility(View.GONE);
            } else if ("1".equals(tagList.get(position).getSource()) && "0".equals(tagList.get(position).getLeafNode())) {
                holder.mIcon.setImageResource(R.drawable.pt_src);
                holder.mArrow.setVisibility(View.VISIBLE);
            } else if ("1".equals(tagList.get(position).getSource()) && "1".equals(tagList.get(position).getLeafNode())) {
                holder.mIcon.setImageResource(R.drawable.pt_leaf);
                holder.mArrow.setVisibility(View.GONE);
            } else if ("2".equals(tagList.get(position).getSource()) && "0".equals(tagList.get(position).getLeafNode())) {
                holder.mIcon.setImageResource(R.drawable.pt_src);
                holder.mArrow.setVisibility(View.VISIBLE);
            } else if ("2".equals(tagList.get(position).getSource()) && "1".equals(tagList.get(position).getLeafNode())) {
                holder.mIcon.setImageResource(R.drawable.pt_leaf);
                holder.mArrow.setVisibility(View.GONE);
            }
            holder.mRelCategory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ((Integer.parseInt(tagList.get(position).getLeafNode()) == 0 && Integer.parseInt(tagList.get(position).getLevel()) != 6) || tagList.get(position).getMetaCategoryUkid().equals(BaseApplication.sp.getValue("flashData"))) {
                        EventBus.getDefault().post(new CategoryEvent(tagList.get(position), "next"));
                    } else {
                        EventBus.getDefault().post(new CategoryEvent(tagList.get(position), "finish"));
                    }
                }
            });
                holder.mRelCategory.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (onItemLongclickListener != null) {
                            onItemLongclickListener.onLongClick(holder.itemView, position);
                        }
                        return true;
                    }
                });

        } catch (Exception e) {

        }

    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        ImageView mArrow;
        ImageView mIcon;
        RelativeLayout mRelCategory;


        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv_category);
            mIcon = (ImageView) view.findViewById(R.id.iv_icon);
            mArrow = (ImageView) view.findViewById(R.id.iv_arrow);
            mRelCategory = (RelativeLayout) view.findViewById(R.id.rel_category);
        }
    }
     public void setOnItemLongClickListener(OnItemLongclickListener onItemLongclickListener){
         this.onItemLongclickListener = onItemLongclickListener;
     }

     public interface OnItemLongclickListener{
        void  onLongClick(View view,int position);
     }

}
